package com.huajin.iot.system.controller;

import com.huajin.iot.common.utils.R;
import com.huajin.iot.system.domain.UserOnline;
import com.huajin.iot.system.service.SessionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = {"session接口"})
@RequestMapping("/sys/online")
@RestController
public class SessionController {
    @Autowired
    SessionService sessionService;


    @ApiOperation("列出所有在线用户")
    @ResponseBody
    @GetMapping("/list")
    public R list() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", sessionService.list());
        return R.ok(resultMap);
    }

    @ApiOperation("强制账户推出")
    @ResponseBody
    @GetMapping("/forceLogout/{sessionId}")
    public R forceLogout(@PathVariable("sessionId") String sessionId, RedirectAttributes redirectAttributes) {
        try {
            sessionService.forceLogout(sessionId);
            return R.ok();
        } catch (Exception e) {
            e.printStackTrace();
            return R.error();
        }

    }

    @ApiOperation("所有登录session")
    @ResponseBody
    @GetMapping("/sessionList")
    public R sessionList() {
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("list", sessionService.sessionList());
        return R.ok(resultMap);
    }
}
